package com.bocai.dahuatech.ui.frag;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bocai.dahuatech.Bean.BaseBean;
import com.bocai.dahuatech.Bean.ProductSeriesListBean_new;
import com.bocai.dahuatech.R;
import com.bocai.dahuatech.base.BaseFragment;
import com.bocai.dahuatech.ui.activity.ProductSecendActivity;
import com.bocai.dahuatech.ui.activity.ProductThirdActivity;
import com.bocai.dahuatech.ui.activity.SearchProductActivity;
import com.bocai.dahuatech.utils.AESUtils;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 产品1级
 */
public class Fragment_Product extends BaseFragment implements View.OnClickListener{
    private View view;
    ProductSeriesListBean_new bean;
    @Bind(R.id.swipe_product_third)
    SwipeRefreshLayout swipeProduct;
    @Bind(R.id.title_tv)
    TextView tvTitle;
    @Bind(R.id.ll_title_right)
    LinearLayout llTitle;
    @Bind(R.id.et_search)
    EditText etSearch;
    @Bind(R.id.ll_productfirst_one)
    LinearLayout llOne;
    @Bind(R.id.ll_productfirst_two)
    LinearLayout llTwo;
    @Bind(R.id.ll_productfirst_three)
    LinearLayout llThree;
    @Bind(R.id.ll_productfirst_four)
    LinearLayout llFour;
    @Bind(R.id.ll_productfirst_five)
    LinearLayout llFive;
    @Bind(R.id.ll_productfirst_six)
    LinearLayout llSix;
    @Bind(R.id.ll_productfirst_seven)
    LinearLayout llSeven;
    @Bind(R.id.iv_productfirst_one)
    ImageView ivOne;
    @Bind(R.id.iv_productfirst_two)
    ImageView ivTwo;
    @Bind(R.id.iv_productfirst_three)
    ImageView ivThree;
    @Bind(R.id.iv_productfirst_four)
    ImageView ivFour;
    @Bind(R.id.iv_productfirst_five)
    ImageView ivFive;
    @Bind(R.id.iv_productfirst_six)
    ImageView ivSix;
    @Bind(R.id.iv_productfirst_seven)
    ImageView ivSeven;
    @Bind(R.id.tv_productfirst_title_one)
    TextView tvTitleOne;
    @Bind(R.id.tv_productfirst_content_one)
    TextView tvContentOne;
    @Bind(R.id.tv_productfirst_two)
    TextView tvTwo;
    @Bind(R.id.tv_productfirst_three)
    TextView tvThree;
    @Bind(R.id.tv_productfirst_title_four)
    TextView tvTitleFour;
    @Bind(R.id.tv_productfirst_content_four)
    TextView tvContentFour;
    @Bind(R.id.tv_productfirst_title_five)
    TextView tvTitleFive;
    @Bind(R.id.tv_productfirst_content_five)
    TextView tvContentFive;
    @Bind(R.id.tv_productfirst_six)
    TextView tvSix;
    @Bind(R.id.tv_productfirst_seven)
    TextView tvSeven;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
    {
        view = inflater.inflate(R.layout.fragment_productfirst_new, container, false);
        initView(view);
        return view;
    }

    private void initView(View view)
    {
        ButterKnife.bind(this, view);
        tvTitle.setText(R.string.product);
        llTitle.setVisibility(View.GONE);
        etSearch.setHint(R.string.productserchhint);
        etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    Intent intent = new Intent(getActivity(), SearchProductActivity.class);
                    intent.putExtra("key", etSearch.getText().toString());
                    intent.putExtra("what", 1);
                    startActivity(intent);
                }
                return false;
            }
        });
        getDatas();
        refresh();

    }

    private void refresh() {
        swipeProduct.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener()
        {
            @Override
            public void onRefresh()
            {
                getDatas();
            }
        });
    }

    private void getDatas()
    {
        getTestApi().getProductSeriesList(1,0)
                .compose(this.<BaseBean>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
    Observer<BaseBean> observer=new Observer<BaseBean>()
    {
        @Override
        public void onCompleted()
        {

        }

        @Override
        public void onError(Throwable e)
        {
            swipeProduct.setRefreshing(false);
            hideLoading();
            showToast(R.string.neterror);
            Log.e("error", e.toString());
        }

        @Override
        public void onNext(BaseBean baseBean) {
            swipeProduct.setRefreshing(false);
            if (baseBean.getReturnNo().equals("1"))
            {
                initEvent();
                String result= AESUtils.decode(baseBean.getContent());
                result=start+result+end;
                Log.e("result",result);
                bean=new Gson().fromJson(result,ProductSeriesListBean_new.class);
                setImag();
            }else
            {
                showToast(baseBean.getReturnInfo());
            }
        }
    };

    private void setImag()
    {
        Glide.with(this).load(bean.getContent().get(0).getFImgSrc()).into(ivOne);
        Glide.with(this).load(bean.getContent().get(1).getFImgSrc()).into(ivTwo);
        Glide.with(this).load(bean.getContent().get(2).getFImgSrc()).into(ivThree);
        Glide.with(this).load(bean.getContent().get(3).getFImgSrc()).into(ivFour);
        Glide.with(this).load(bean.getContent().get(4).getFImgSrc()).into(ivFive);
        Glide.with(this).load(bean.getContent().get(5).getFImgSrc()).into(ivSix);
        Glide.with(this).load(bean.getContent().get(6).getFImgSrc()).into(ivSeven);
        tvTitleOne.setText(bean.getContent().get(0).getFItemName());
        tvContentOne.setText(bean.getContent().get(0).getFItemFullName());
        tvTwo.setText(bean.getContent().get(1).getFItemName());
        tvThree.setText(bean.getContent().get(2).getFItemName());
        tvTitleFour.setText(bean.getContent().get(3).getFItemName());
        tvContentFour.setText(bean.getContent().get(3).getFItemFullName());
        tvTitleFive.setText(bean.getContent().get(4).getFItemName());
        tvContentFive.setText(bean.getContent().get(4).getFItemFullName());
        tvSix.setText(bean.getContent().get(5).getFItemName());
        tvSeven.setText(bean.getContent().get(6).getFItemName());
    }
    protected void initEvent() {
        llOne.setOnClickListener(this);
        llTwo.setOnClickListener(this);
        llThree.setOnClickListener(this);
        llFour.setOnClickListener(this);
        llFive.setOnClickListener(this);
        llSix.setOnClickListener(this);
        llSeven.setOnClickListener(this);
    }
    Intent intent=null;
    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.ll_productfirst_one:
                /**
                 * 值为1时，标示下面没有分类，直接到产品列表，为0时，标示下面含有多个分类
                 */
                if(bean.getContent().get(0).getFIsDetail()==0)
                {
                    intent=new Intent(getActivity(), ProductSecendActivity.class);
                    intent.putExtra("text",bean.getContent().get(0).getFItemName());
                    intent.putExtra("parentId",bean.getContent().get(0).getFID());
                    startActivity(intent);
                }else
                {

                    intent=new Intent(getActivity(),ProductThirdActivity.class);
                    intent.putExtra("text",bean.getContent().get(0).getFItemName());
                    intent.putExtra("fid",bean.getContent().get(0).getFRelatedID());
                    intent.putExtra("key","");
                    intent.putExtra("screetype",bean.getContent().get(0).getFScreenType());
                    startActivity(intent);
                }
                break;
            case R.id.ll_productfirst_two:
                if(bean.getContent().get(1).getFIsDetail()==0)
                {
                    intent=new Intent(getActivity(), ProductSecendActivity.class);
                    intent.putExtra("text",bean.getContent().get(1).getFItemName());
                    intent.putExtra("parentId",bean.getContent().get(1).getFID());
                    startActivity(intent);
                }else
                {
                    intent=new Intent(getActivity(),ProductThirdActivity.class);
                    intent.putExtra("text",bean.getContent().get(1).getFItemName());
                    intent.putExtra("fid",bean.getContent().get(1).getFRelatedID());
                    intent.putExtra("key","");
                    intent.putExtra("screetype",bean.getContent().get(1).getFScreenType());
                    startActivity(intent);
                }
                break;
            case R.id.ll_productfirst_three:
                if(bean.getContent().get(2).getFIsDetail()==0)
                {
                    intent=new Intent(getActivity(), ProductSecendActivity.class);
                    intent.putExtra("text",bean.getContent().get(2).getFItemName());
                    intent.putExtra("parentId",bean.getContent().get(2).getFID());
                    startActivity(intent);
                }else
                {
                    intent=new Intent(getActivity(),ProductThirdActivity.class);
                    intent.putExtra("text",bean.getContent().get(2).getFItemName());
                    intent.putExtra("fid",bean.getContent().get(2).getFRelatedID());
                    intent.putExtra("key","");
                    intent.putExtra("screetype",bean.getContent().get(2).getFScreenType());
                    startActivity(intent);
                }
                break;
            case R.id.ll_productfirst_four:
                if(bean.getContent().get(3).getFIsDetail()==0)
                {
                    intent=new Intent(getActivity(), ProductSecendActivity.class);
                    intent.putExtra("text",bean.getContent().get(3).getFItemName());
                    intent.putExtra("parentId",bean.getContent().get(3).getFID());
                    startActivity(intent);
                }else
                {
                    intent=new Intent(getActivity(),ProductThirdActivity.class);
                    intent.putExtra("text",bean.getContent().get(3).getFItemName());
                    intent.putExtra("fid",bean.getContent().get(3).getFRelatedID());
                    intent.putExtra("key","");
                    intent.putExtra("screetype",bean.getContent().get(3).getFScreenType());
                    startActivity(intent);
                }
                break;
            case R.id.ll_productfirst_five:
                if(bean.getContent().get(4).getFIsDetail()==0)
                {
                    intent=new Intent(getActivity(), ProductSecendActivity.class);
                    intent.putExtra("text",bean.getContent().get(4).getFItemName());
                    intent.putExtra("parentId",bean.getContent().get(4).getFID());
                    startActivity(intent);
                }else
                {
                    intent=new Intent(getActivity(),ProductThirdActivity.class);
                    intent.putExtra("text",bean.getContent().get(4).getFItemName());
                    intent.putExtra("fid",bean.getContent().get(4).getFRelatedID());
                    intent.putExtra("key","");
                    intent.putExtra("screetype",bean.getContent().get(4).getFScreenType());
                    startActivity(intent);
                }
                break;
            case R.id.ll_productfirst_six:
                if(bean.getContent().get(5).getFIsDetail()==0)
                {
                    intent=new Intent(getActivity(), ProductSecendActivity.class);
                    intent.putExtra("text",bean.getContent().get(5).getFItemName());
                    intent.putExtra("parentId",bean.getContent().get(5).getFID());
                    startActivity(intent);
                }else
                {
                    intent=new Intent(getActivity(),ProductThirdActivity.class);
                    intent.putExtra("text",bean.getContent().get(5).getFItemName());
                    intent.putExtra("fid",bean.getContent().get(5).getFRelatedID());
                    intent.putExtra("key","");
                    intent.putExtra("screetype",bean.getContent().get(5).getFScreenType());
                    startActivity(intent);
                }
                break;
            case R.id.ll_productfirst_seven:
                if(bean.getContent().get(6).getFIsDetail()==0)
                {
                    intent=new Intent(getActivity(), ProductSecendActivity.class);
                    intent.putExtra("text",bean.getContent().get(6).getFItemName());
                    intent.putExtra("parentId",bean.getContent().get(6).getFID());
                    startActivity(intent);
                }else
                {
                    intent=new Intent(getActivity(),ProductThirdActivity.class);
                    intent.putExtra("text",bean.getContent().get(6).getFItemName());
                    intent.putExtra("fid",bean.getContent().get(6).getFRelatedID());
                    intent.putExtra("key","");
                    intent.putExtra("screetype",bean.getContent().get(6).getFScreenType());
                    startActivity(intent);
                }
                break;
        }
    }
}
